import { defineStore } from "pinia";
import { categoryData } from "./interface/index";
import {
  reqGetCategory1List,
  reqGetCategory2List,
  reqGetCategory3List,
} from "@/api/product/getCategory";
export const useCategoryStore = defineStore("category", {
  //state函数收集数据
  state: (): categoryData => ({
    category1List: [],
    category2List: [],
    category3List: [],
    category1Id: undefined,
    category2Id: undefined,
    category3Id: undefined,
  }),
  //计算
  getters: {},
  //发送异步求和加工数据
  actions: {
    //发送请求获取一级数据
    async getCategory1List() {
      const result1 = await reqGetCategory1List();
      this.category1List = result1;
    },
    //发送请求获取二级数据
    async getCategory2List() {
      const result2 = await reqGetCategory2List(this.category1Id as number);
      this.category2List = result2;
      //当一级发生改变时需要清空二三级数据
      this.category3List = [];
      this.category2Id = undefined;
      this.category3Id = undefined;
    },

    //发送请求获取三级数据
    async getCategory3List() {
      const result3 = await reqGetCategory3List(this.category2Id as number);
      this.category3List = result3;
      this.category3Id = undefined;
    },
  },
});
